package com.woniu108.car.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu108.carService.commos.dto.CarInfoDto;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import com.woniu108.car.form.InsertCarInfoForm;
import com.woniu108.car.form.UpdateCarInfoForm;
import com.woniu108.car.param.DeleteCarInfoParam;
import com.woniu108.car.param.InsertCarInfoParam;
import com.woniu108.car.param.UpdateCarInfoParam;
import com.woniu108.car.service.CarInfoService;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 车辆信息表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/carInfo")
@Api(tags = "汽车详情管理")
public class CarInfoController {
    @Resource
    private CarInfoService carInfoService;
    @Resource
    private RedisUtil redisUtil;
    /**
     *  查询全部汽车信息
     * @return 汽车信息dto集合
     */
    @GetMapping("/loadAllCarInfo.api")
    @ApiOperation("查询所有汽车详情")
    public Result<List<CarInfoDto>> loadAllCarInfo(@RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        List<CarInfoDto> list = carInfoService.loadAllCarInfo(adminId);
        return Result.newListInstance(CarInfoDto.class).setData(list).setMsg("查询所有汽车详情成功");
    }
    /**
     *  根据用户id查汽车详情
     * @return 汽车信息dto集合
     */
    @GetMapping("/loadCarInfoByUserId")
    @ApiOperation("根据用户id查汽车详情")
    public Result<List<CarInfoDto>> loadCarInfoByUserId(@RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String userId = JWTUtil.parseUserToken(refreshToken).get("userId").toString();
        List<CarInfoDto> list = carInfoService.loadCarInfoByUserId(userId);
        return Result.newListInstance(CarInfoDto.class).setData(list).setMsg("根据用户id查汽车详情成功");
    }

    /**
     *  根据用户id查汽车详情（非token）
     * @return 汽车信息dto集合
     */
    @GetMapping("/loadCarInfoById/{userId}")
    @ApiOperation("根据用户id查汽车详情（非token）")
    public Result<List<CarInfoDto>> loadCarInfoById(@PathVariable("userId")@ApiParam("用户id") String userId) {
        List<CarInfoDto> list = carInfoService.loadCarInfoByUserId(userId);
        return Result.newListInstance(CarInfoDto.class).setData(list).setMsg("根据用户id查汽车详情成功");
    }

    /**
     *  新增用户汽车详情
     * @param form  汽车详情
     * @return boolean值
     */
    @PostMapping("/insertCarInfo")
    @ApiOperation("新增用户汽车详情")
    public Result<Boolean> insertCarInfo(@RequestBody InsertCarInfoForm form, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String userId = JWTUtil.parseUserToken(refreshToken).get("userId").toString();
        InsertCarInfoParam param = BeanUtil.toBean(form, InsertCarInfoParam.class);
        param.setUserId(userId);
        Boolean bool = carInfoService.insertCarInfo(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("新增用户汽车详情成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("新增用户汽车详情失败").setData(false).setCode(500);
    }
    /**
     *  修改用户汽车详情
     * @param form  汽车id，公里数
     * @return boolean值
     */
    @PostMapping("/updateCarInfo")
    @ApiOperation("修改用户汽车详情")
    public Result<Boolean> updateCarInfo(@RequestBody UpdateCarInfoForm form, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String userId = JWTUtil.parseUserToken(refreshToken).get("userId").toString();
        UpdateCarInfoParam param = BeanUtil.toBean(form, UpdateCarInfoParam.class);
        param.setUserId(userId);
        Boolean bool = carInfoService.updateCarInfo(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("修改用户汽车详情成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("修改用户汽车详情失败").setData(false).setCode(500);
    }
    /**
     *  删除用户汽车详情
     * @param carId  汽车id
     * @return boolean值
     */
    @GetMapping("deleteCarInfo")
    @ApiOperation("删除用户汽车详情")
    public Result<Boolean> deleteCarInfo(Integer carId, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String userId = JWTUtil.parseUserToken(refreshToken).get("userId").toString();
        DeleteCarInfoParam param=new DeleteCarInfoParam(carId,userId);
        Boolean bool = carInfoService.deleteCarInfo(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("删除用户汽车详情成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("删除用户汽车详情失败").setData(false).setCode(500);

    }
}

